## ---------------------------
##
## Script name: process_city_geographies.R
##
## Description: 
##
## ---------------------------
## DEPENDENCIES

library(conflicted)
conflict_prefer("lag", "dplyr")

## ---------------------------


.generate_rolling_5yrmean_demographics <- function(demos) {
  #'
  #'@description takes a 5-year rolling mean of tract 
  #'demographics to smooth out year-to-year noise in demographic
  #'changes.
  #'
  #'@param demos dataframe. Dataframe with tract by year demographics
  #'and population data.
  #'
  #'@return dataframe. same structure as input dataframe
  #'but population values by year are rolling averages.
  
  df <- demos %>%
        arrange(tract_2010, year) %>%
        group_by(tract_2010) %>%
        mutate(total_population_lag1 = ifelse(is.na(lag(total_population)), 0, lag(total_population)),
               total_population_lag2 = ifelse(is.na(lag(total_population, 2)), 0, lag(total_population, 2)),
               total_population_lead1 = ifelse(is.na(lead(total_population)), 0, lead(total_population)),
               total_population_lead2 = ifelse(is.na(lead(total_population, 2)), 0, lead(total_population, 2)),
               total_population_rollingsum5yrs = total_population_lag2 + total_population_lag1 + total_population + 
                 total_population_lead1 + total_population_lead2) %>%
        mutate(total_population_white_lag1 = ifelse(is.na(lag(total_population_white)), 0, lag(total_population_white)),
               total_population_white_lag2 = ifelse(is.na(lag(total_population_white, 2)), 0, lag(total_population_white, 2)),
               total_population_white_lead1 = ifelse(is.na(lead(total_population_white)), 0, lead(total_population_white)),
               total_population_white_lead2 = ifelse(is.na(lead(total_population_white, 2)), 0, lead(total_population_white, 2)),
               total_population_white_rollingsum5yrs = total_population_white_lag2 + total_population_white_lag1 + total_population_white + 
                 total_population_white_lead1 + total_population_white_lead2) %>%
        mutate(total_population_black_lag1 = ifelse(is.na(lag(total_population_black)), 0, lag(total_population_black)),
               total_population_black_lag2 = ifelse(is.na(lag(total_population_black, 2)), 0, lag(total_population_black, 2)),
               total_population_black_lead1 = ifelse(is.na(lead(total_population_black)), 0, lead(total_population_black)),
               total_population_black_lead2 = ifelse(is.na(lead(total_population_black, 2)), 0, lead(total_population_black, 2)),
               total_population_black_rollingsum5yrs = total_population_black_lag2 + total_population_black_lag1 + total_population_black + 
                 total_population_black_lead1 + total_population_black_lead2) %>%
        mutate(total_below_pov_level_lag1 = ifelse(is.na(lag(total_below_pov_level)), 0, lag(total_below_pov_level)),
               total_below_pov_level_lag2 = ifelse(is.na(lag(total_below_pov_level, 2)), 0, lag(total_below_pov_level, 2)),
               total_below_pov_level_lead1 = ifelse(is.na(lead(total_below_pov_level)), 0, lead(total_below_pov_level)),
               total_below_pov_level_lead2 = ifelse(is.na(lead(total_below_pov_level, 2)), 0, lead(total_below_pov_level, 2)),
               total_below_pov_level_rollingsum5yrs = total_below_pov_level_lag2 + total_below_pov_level_lag1 + total_below_pov_level + 
                 total_below_pov_level_lead1 + total_below_pov_level_lead2) %>%
        dplyr::select(tract_2010, year, total_population, total_population_rollingsum5yrs, total_population_white_rollingsum5yrs, total_population_black_rollingsum5yrs,
                      total_below_pov_level_rollingsum5yrs) %>%
        mutate(pop_pov = total_below_pov_level_rollingsum5yrs/total_population_rollingsum5yrs,
               pop_b = total_population_black_rollingsum5yrs/total_population_rollingsum5yrs,
               pop_w = total_population_white_rollingsum5yrs/total_population_rollingsum5yrs) %>%
        mutate(poverty_bin = ifelse(pop_pov >= 0.3, "poor", "nonpoor"),
               majority_race_bin = ifelse(pop_b >= 0.5, "black", 
                                   ifelse(pop_w >= 0.5, "white",
                                          ifelse(pop_b < 0.5 & pop_w < 0.5, "allotherrace", NA))))
  
  return(df)
  
}